//
//  MainDetailTopScrollNewV.m
//  ZITHER
//
//  Created by weixin on 2018/9/2.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainDetailTopScrollNewV.h"

@implementation MainDetailTopScrollNewV

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration] ;
        [self setUI];
        [self setMasonry];
    }
    return self;
}

-(void)defaultConfiguration{
    self.bounces = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.contentSize = CGSizeMake(kScreenW*3,0);
    self.contentOffset = CGPointMake(kScreenW, 0);
    self.bounces = NO; // 默认为YES 取消设置NO
    // 当bounces属性为NO时，以下两个属性设置无效
    self.alwaysBounceHorizontal = YES; // 水平方向弹簧效果
    self.alwaysBounceVertical = YES; // 垂直方向弹簧效果
    self.decelerationRate = 0.1; // 值域 0~1 值越小减速停止的时间越短
    self.pagingEnabled = YES; // 默认为NO，设置YES内容视图整页滚动
    self.delegate = self;
    self.backgroundColor = [UIColor whiteColor];
}

-(void)setUI{
    [self addSubview:self.imageV];
    [self addSubview:self.viewBrand];
    [self.viewBrand addSubview:self.brandName];
    [self.viewBrand addSubview:self.brandNameNative];
    [self addSubview:self.viewSeries];
    [self.viewSeries addSubview:self.seriesName];
    [self.viewSeries addSubview:self.seriesNameNative];
    [self addSubview:self.tbvTitle];
    
}
-(void)setMasonry{
    
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self).offset(0);
        make.width.height.mas_equalTo(kScreenW);
    }];
    

    [self.viewBrand mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(0);
        make.left.mas_equalTo(self).offset(kScreenW);
        make.width.mas_equalTo(kScreenW);
        make.height.mas_equalTo(80);
    }];

    [self.brandNameNative mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.viewBrand).offset(15);
        make.left.mas_equalTo(self.viewBrand).offset(80);
    }];

    [self.brandName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.viewBrand).offset(-15);
        make.left.mas_equalTo(self.viewBrand).offset(80);
    }];

    
    [self.viewSeries mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(80);
        make.left.mas_equalTo(self).offset(kScreenW);
        make.width.mas_equalTo(kScreenW);
        make.height.mas_equalTo(80);
    }];

    [self.seriesNameNative mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.viewSeries).offset(15);
        make.left.mas_equalTo(self.viewSeries).offset(80);
    }];
    
    [self.seriesName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.viewSeries).offset(-15);
        make.left.mas_equalTo(self.viewSeries).offset(80);
    }];
    
    [self.tbvTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(0);
        make.left.mas_equalTo(self).offset(kScreenW*2);
        make.width.height.mas_equalTo(kScreenW);
    }];
    

    for (int i =0; i<2; i++) {
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 30, 30, 30)];
        
        UIImageView *imageV2= [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-40, 30, 25, 25)];
        imageV2.image = [UIImage imageNamed:@"sku_btn_into"];
        imageV.contentMode = 2;

        if (i == 0) {
            imageV.image = [UIImage imageNamed:@"sku_icon_brand"];
            [self.viewBrand addSubview:imageV];
            [self.viewBrand addSubview:imageV2];
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.viewBrand).offset(0);
                make.left.mas_equalTo(self.viewBrand).offset(24);
                make.width.height.mas_equalTo(20);
            }];
            [imageV2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.viewBrand).offset(0);
                make.right.mas_equalTo(self.viewBrand).offset(-14);
                make.width.height.mas_equalTo(25);
            }];
        }else{
            imageV.image = [UIImage imageNamed:@"sku_icon_serise"];
            [self.viewSeries addSubview:imageV];
            [self.viewSeries addSubview:imageV2];
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.viewSeries).offset(0);
                make.left.mas_equalTo(self.viewSeries).offset(24);
                make.width.height.mas_equalTo(20);
            }];
            [imageV2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.viewSeries).offset(0);
                make.right.mas_equalTo(self.viewSeries).offset(-14);
                make.width.height.mas_equalTo(25);
            }];
        }
    }
}

-(void)setModel:(skuDetailModel *)model
{
    _model = model;
    self.brandName.text = model.brandName;
    self.brandNameNative.text = model.brandNameNative;
    self.seriesName.text = model.seriesName;
    self.seriesNameNative.text = model.seriesNameNative;
    self.tbvTitle.model = model;
    NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,model.imageDefault];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:str]];
}

-(UIImageView *)imageV{
    if (!_imageV) {
        _imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,kScreenW)];
        _imageV.backgroundColor = [UIColor whiteColor];
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        _imageV.image = [UIImage imageNamed:@"watchImage"];
        _imageV.clipsToBounds = YES;
        _imageV.contentMode = 1;
    }
    return  _imageV;
}

-(UIView *)viewBrand{
    if (!_viewBrand) {
        _viewBrand = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,kScreenW)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            NSLog(@"跳转到搜索页");
        }];
        [_viewBrand addGestureRecognizer:tap];
    }
    return  _viewBrand;
}

-(UILabel *)brandName{
    if (!_brandName) {
        _brandName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        _brandName.text = @"Vacheron Constantin";
        _brandName.font = [UIFont systemFontOfSize:25];
    }
    return _brandName;
}

-(UILabel *)brandNameNative{
    if (!_brandNameNative) {
        _brandNameNative = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        _brandNameNative.text = @"江诗丹顿/康斯坦丁";
        _brandNameNative.font = [UIFont systemFontOfSize:15];
    }
    return _brandNameNative;
}

-(UIView *)viewSeries{
    if (!_viewSeries) {
        _viewSeries = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,kScreenW)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            NSLog(@"跳转到搜索页");
        }];
        [_viewSeries addGestureRecognizer:tap];
    }
    return  _viewSeries;
}

-(UILabel *)seriesName{
    if (!_seriesName) {
        _seriesName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        _seriesName.text = @"Vacheron Constantin";
        _seriesName.font = [UIFont systemFontOfSize:25];
    }
    return _seriesName;
}

-(UILabel *)seriesNameNative{
    if (!_seriesNameNative) {
        _seriesNameNative = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        _seriesNameNative.text = @"江诗丹顿/康斯坦丁";
        _seriesNameNative.font = [UIFont systemFontOfSize:15];
    }
    return _seriesNameNative;
}

-(MainDetailTopPropertyTbv *)tbvTitle{
    if (!_tbvTitle) {
        _tbvTitle = [[MainDetailTopPropertyTbv alloc]initWithFrame:CGRectMake(kScreenW*2,0 , kScreenW,SCREEN_WIDTH-30)];
    }
    return  _tbvTitle;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x == 0) {
        [self changePage:0];
    }else if (scrollView.contentOffset.x == SCREEN_WIDTH){
        [self changePage:1];
    }else if (scrollView.contentOffset.x == SCREEN_WIDTH*2){
        [self changePage:2];
    }
    
}

-(void)changePage:(int)index{
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        if (index == 1) {
            make.height.mas_equalTo(190);
        }else{
            make.height.mas_equalTo(kScreenW);
        }
    }];
//    SKUDetailNewVc *vc = (SKUDetailNewVc*)[self getViewController:[SKUDetailNewVc class]];
//    vc.scrBasepParent.DetailWatchScr.pageController.currentPage = index;

    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];

    [UIView animateWithDuration:0.2 animations:^{
//        [vc.view layoutIfNeeded];
//        CGFloat height = CGRectGetMaxY(vc.scrBasepParent.DetailWatchScr.GraphV.frame);
//        vc.scrBasepParent.DetailWatchScr.contentSize = CGSizeMake(0,height);
    }];

}





@end
